<template>
  <div>
    <el-form
      :model="recordData"
      ref="form"
      label-width="80px"
      :inline="false"
      size="default"
      :props="{
        label: (data) => {
          console.log('data', data)
          // return data.meta.title
        }
      }"
    >
      <el-form-item label="发生时间">
        <el-input v-model="recordData.beginTime" />
      </el-form-item>
      <el-form-item label="救援人员">
        <el-input v-model="recordData.person" />
      </el-form-item>
      <el-form-item label="结束时间">
        <el-input v-model="recordData.endTime" />
      </el-form-item>
      <el-form-item label="发生地点" prop="position">
        <div class="flex flex-1">
          <el-input v-model="recordData.position" readonly placeholder="请点击按钮进行地图选址" />
          <el-button type="primary" size="default" @click="drawer = true">地图选址</el-button>
        </div>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="recordData.remark" />
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="onSubmit" v-if="edata">确认修改</el-button>
        <el-button type="primary" @click="onSubmit" v-else>立即新增</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
    <el-drawer title="地图选址" v-model="drawer" direction="btt" size="60%">
      <GodMap v-model="recordData.position" />
    </el-drawer>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { recordDataType } from '../types'
import { fireRecorPost, fireRecorPut } from '@/api/FireWarning'
import { onMounted } from 'vue'
import GodMap from './god_map.vue'
const emit = defineEmits()
const recordData: recordDataType = reactive({
  beginTime: '',
  endTime: '',
  person: '',
  position: '',
  remark: ''
})

// 新增
const onSubmit = () => {
  // fireFacilityPost(facilityData).then((res) => {
  //     emit('update:table-data', {
  //         ...facilityData,
  //         objectId: res.data.objectId
  //     })
  // })

  if (props.edata) {
    // 修改
    let { objectId } = props.edata
    fireRecorPut(objectId, recordData).then(() => {
      emit('update:table-data', {
        ...recordData,
        objectId
      })
    })
  } else {
    //新增
    fireRecorPost(recordData).then((res) => {
      emit('update:table-data', {
        ...recordData,
        objectId: res.data.objectId
      })
    })
  }
}
// 地图选址弹窗
const drawer = ref<boolean>(false)
// props数据具备单向性
type PropsType = {
  edata: recordDataType | null
}
const props = defineProps<PropsType>()
console.log('接收到了父级的props', props.edata)
onMounted(() => {
  if (props.edata) {
    //点击到了编辑按钮
    // console.log(props.edata, 99999);
    let { beginTime, endTime, person, position, remark } = props.edata
    // console.log(1212,name);

    recordData.beginTime = beginTime
    recordData.position = position
    recordData.endTime = endTime
    recordData.remark = remark
    recordData.person = person

    // 修改
    // parkHealthPut(props.edata.objectId, roleDate)
  } else {
    //点击到了新增按钮

    console.log('else')
    recordData.beginTime = ''
    recordData.position = ''
    recordData.endTime = ''
    recordData.remark = ''
    recordData.person = ''
  }
})
</script>

<style scoped></style>
